import 'package:flutter/material.dart';

import '../../../config/global_config.dart';
import '../bean/login_info.dart';

class AndroidTextField extends StatelessWidget {
  Map info;
  static bool _isChange = false;
  bool isEmptyPWD;

  // 用户名、密码输入框的控制器
  static TextEditingController account = TextEditingController();
  static TextEditingController password = TextEditingController();

  static final _accountFocus = FocusNode();
  static final _passwordFocus = FocusNode();

  AndroidTextField(Map info, {bool isEmptyPWD})
      : this.info = info,
        this.isEmptyPWD = isEmptyPWD;

  // 账户：zhangshier
  // 密码：123456

  @override
  Widget build(BuildContext context) {
    String accountValue = _isChange
        ? account.text
        : info["account"].isEmpty
            ? "zhangshier"
            : info["account"];
    String passwordValue = _isChange
        ? password.text
        : info["password"].isEmpty
            ? "123456"
            : info["password"];

    // 获取account值
    account.value = _TextEditingValue(accountValue);
    password.value =
        isEmptyPWD ? _TextEditingValue("") : _TextEditingValue(passwordValue);

    // 创建widget
    return Container(
      width: double.infinity,
      child: Column(
        children: [
          Listener(
            onPointerDown: (e) =>
                FocusScope.of(context).requestFocus(_accountFocus),
            child: TextField(
              controller: account,
              focusNode: _accountFocus,
              keyboardType: TextInputType.text,
              textInputAction: TextInputAction.done,
              //keyboardAppearance: Brightness.dark,
              cursorColor: GlobalConfig.theme,
              cursorWidth: 1,
              decoration: InputDecoration(
                hintText: '请输入账户',
                filled: true,
                fillColor: Color.fromRGBO(255, 255, 255, .8),
                hintStyle: TextStyle(color: GlobalConfig.assistColor),
                prefixIcon: Icon(Icons.account_box),
                enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: GlobalConfig.borderColor),
                ),
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: GlobalConfig.theme),
                ),
              ),
              onChanged: (String v) {
                if (!_isChange) _isChange = true;
              },
            ),
          ),
          Listener(
            onPointerDown: (e) =>
                FocusScope.of(context).requestFocus(_passwordFocus),
            child: TextField(
              controller: password,
              focusNode: _passwordFocus,
              keyboardType: TextInputType.text,
              textInputAction: TextInputAction.done,
              //keyboardAppearance: Brightness.dark,
              cursorColor: GlobalConfig.theme,
              cursorWidth: 1,
              obscureText: true,
              decoration: InputDecoration(
                hintText: '请输入密码',
                filled: true,
                fillColor: Color.fromRGBO(255, 255, 255, .8),
                hintStyle: TextStyle(color: GlobalConfig.assistColor),
                prefixIcon: Icon(Icons.keyboard),
                enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: GlobalConfig.borderColor),
                ),
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: GlobalConfig.theme),
                ),
              ),
              onChanged: (String v) {
                if (!_isChange) _isChange = true;
              },
            ),
          ),
        ],
      ),
    );
  }

  /// 为控制器创建value（此做法防止光标移位）
  /// * [editvalue] String|需要编辑的值|必填
  TextEditingValue _TextEditingValue(String editvalue) {
    return TextEditingValue(
      text: editvalue,
      selection: TextSelection.fromPosition(
        TextPosition(
          affinity: TextAffinity.downstream,
          offset: editvalue.length,
        ),
      ),
    );
  }

  /// 验证结果
  static LoginInfoBean verificaResult() {
    int len = 5;
    String _account = account.text.trim(), _pwd = password.text.trim();

    if (_account.isEmpty) {
      return LoginInfoBean(msg: '账户不能为空');
    }

    if (_account.length < len) {
      return LoginInfoBean(msg: '账户不可小于5位');
    }

    if (_pwd.isEmpty) {
      return LoginInfoBean(msg: '密码不能为空');
    }

    if (_pwd.length < 5) {
      return LoginInfoBean(msg: '密码不可小于5位');
    }

    return LoginInfoBean.fromJson({
      "status": true,
      "data": {"account": account.text, "password": password.text},
      "msg": "通过",
    });
  }
}
